<?php

namespace app\models;

use think\Model;

/**
 * Class CampusModel.
 */
class CampusModel extends Model
{
	protected $table = 'edu_campus';

	/**
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\ModelNotFoundException
	 * @throws \think\exception\DbException
	 *
	 * @return string
	 */
	public static function getIds()
	{
		$campus = self::select()->toArray();
		$arr = [];
		foreach ($campus as $cap) {
			$arr[] = $cap['id'];
		}

		return implode(',', $arr);
	}

	/**
	 * @param $id
	 * @param string $type
	 *
	 * @throws \think\exception\DbException
	 *
	 * @return null|array|AuthGroupAccessModel
	 */
	public static function getById($id, $type = 'all')
	{
		if (!is_numeric($id)) {
			return [];
		}
		$campus = self::get(['id' => $id]);
		if ($campus) {
			return (isset($campus[$type])) ? $campus[$type] : $campus;
		}

		return [];
	}

	/**
	 * @param $param
	 *
	 * @throws \think\exception\DbException
	 *
	 * @return array
	 */
	public function getCampusLists($param)
	{
		$page = isset($param['page']) ? (int) $param['page'] : 1;
		$listRows = isset($param['limit']) ? (int) $param['limit'] : 10;
		$where = [];
		if (isset($param['campus_name']) && '' !== $param['campus_name']) {
			$where['campus_name'] = ['like', $param['campus_name'].'%'];
		}
		if (isset($param['status']) && '' !== $param['status']) {
			$where['status'] = $param['status'];
		}
		if (isset($param['regional_id']) && '' !== $param['regional_id']) {
			$where['regional_id'] = $param['regional_id'];
		}
		if(get_current_type() != 1){
			$where['company_id'] = get_current_company_id();
		}
		$listData = self::where($where)->order('update_time desc')
			->paginate($listRows, false, ['page' => $page])
			->toArray();
		if (!empty($listData)) {
			foreach ($listData['data'] as &$item) {
				if (array_key_exists('course_id', $param)) {
					$course_id = $param['course_id'];
					$campusPriceConfigure = CampusPriceConfigureModel::get(['campus_id' => $item['id'],'course_id'=>$course_id]);
					if ($campusPriceConfigure && isset($course_id)) {
						$item['campus_price'] = (CampusPriceConfigureModel::getById($item['id'], $course_id, 'price'))/100;
					}elseif($campusPriceConfigure == '' && isset($course_id)){
						$item['campus_price'] = CourseModel::get($course_id)['price'] / 100;
					}
				}
				$item['status_text'] = (1 === $item['status']) ? '启用' : '禁用';
				$item['regional_name'] = RegionalModel::get($item['regional_id'])['regional_name'];
				$item['company_name'] = CompanyModel::get($item['company_id'])['company_name'];
			}
		}

		return $listData;
	}

	/**
	 * @param $param
	 * @return array
	 * @throws \think\exception\DbException
	 */
	public function getContractCampusLists()
	{
		$where = [];
		if(get_current_type() == 4 || get_current_type() == 5){
			$where['id'] = get_current_campus_id();
		}elseif (get_current_type() == 2 || get_current_type() == 3){
			$where['id'] = ['in',get_current_campus_id()];
		}
		$listData = self::where($where)->select();
		return $listData;
	}

	/**
	 *
	 * 课消报表记录
	 */
	public function getEliminationLists($param){

		$StudentSubjectInfosClassModel = new StudentSubjectInfosClassModel();
		$GradesDetailsModel = new GradesDetailsModel();  // 班级
		$page = isset($param['page']) ? (int) $param['page'] : 1;
		$listRows = isset($param['limit']) ? (int) $param['limit'] : 10;
		$where = ['status' => 1];  //已上课
		$where_time = ['status' => 1]; //已上课
		$where_time_arrear =  ['status' => 1]; //已上课
		$where_sql = ['status' => 1]; //查询条件
		if (isset($param['campus_name']) && '' !== $param['campus_name']) {  //校区名字
			$where['campus_name'] = ['like', '%'.$param['campus_name'].'%'];
		}

		//时间段
		if (isset($param['start_date']) && '' !== $param['start_date'] && isset($param['end_date']) && '' !== $param['end_date']) {
			$where_time['class_date'] = array('between', array($param['start_date'],$param['end_date']));
			$where_time_arrear['class_date'] = array('between', array($param['start_date'],$param['end_date']));
			$where_sql['class_date'] = array('between', array($param['start_date'],$param['end_date']));
		}
		if(get_current_type() == 4 || get_current_type() == 5){
			$where['id'] = get_current_campus_id();
		}elseif (get_current_type() == 2 || get_current_type() == 3){
			$where['id'] = ['in',get_current_campus_id()];
		}

		if(get_current_type() == 5){
			if(get_current_group_type() == 2){  //老师类
				$grades_res = $GradesDetailsModel->where(['user_teacher_id' => get_current_id()])->column('id');
				$grades_res_str = implode(',',$grades_res);
				$where_time['class_subject_id'] = ['in',$grades_res_str];
				$where_time_arrear['class_subject_id'] = ['in',$grades_res_str];
				$where_sql['class_subject_id'] = ['in',$grades_res_str];
			}else if(get_current_group_type() == 3){  //助教类
				$grades_res = $GradesDetailsModel->where(['user_assistant_id' => get_current_id()])->column('id');
				$grades_res_str = implode(',',$grades_res);
				$where_time['class_subject_id'] = ['in',$grades_res_str];
				$where_time_arrear['class_subject_id'] = ['in',$grades_res_str];
				$where_sql['class_subject_id'] = ['in',$grades_res_str];
			}else if(get_current_group_type() == 4){ //班主任
				$grades_res = $GradesDetailsModel->where(['user_master_id' => get_current_id()])->column('id');
				$grades_res_str = implode(',',$grades_res);
				$where_time['class_subject_id'] = ['in',$grades_res_str];
				$where_time_arrear['class_subject_id'] = ['in',$grades_res_str];
				$where_sql['class_subject_id'] = ['in',$grades_res_str];
			}else if(get_current_group_type() == 5){ //教务类
				$grades_res = $GradesDetailsModel->where(['user_edu_id' => get_current_id()])->column('id');
				$grades_res_str = implode(',',$grades_res);
				$where_time['class_subject_id'] = ['in',$grades_res_str];
				$where_time_arrear['class_subject_id'] = ['in',$grades_res_str];
				$where_sql['class_subject_id'] = ['in',$grades_res_str];
			}
		}

		$listData = self::where($where)
			->paginate($listRows, false, ['page' => $page])
			->toArray();

		$elimination_amount = 0;
		$elimination_no_amount = 0;
		$elimination_all_amount = 0;
		$elimination_sum = 0;
		if (!empty($listData)) {
			foreach ($listData['data'] as &$item){
				$where_time['campus_id'] = $item['id'];
				$where_time['surplus_class_elimination'] =  array('egt',0);  //>=0
				$where_time_arrear['campus_id'] = $item['id'];
				$where_time_arrear['surplus_class_elimination'] =  array('lt',0); // <0
				$where_sql['campus_id'] = $item['id'];

				$item['elimination_amount'] = (($StudentSubjectInfosClassModel->where($where_time)->sum('amount_deducted')) / 100);  //课消金额
				$elimination_amount = $elimination_amount + $item['elimination_amount'];

				$item['elimination_no_amount'] = (($StudentSubjectInfosClassModel->where($where_time_arrear)->sum('amount_deducted')) / 100); //欠费课消金额
				$elimination_no_amount = $elimination_no_amount +  $item['elimination_no_amount'];

				$item['elimination_all_amount'] = (($StudentSubjectInfosClassModel->where($where_sql)->sum('amount_deducted')) / 100);  //总金额
				$elimination_all_amount = $elimination_all_amount + $item['elimination_all_amount'];

				$item['elimination_sum'] = $StudentSubjectInfosClassModel->where($where_sql)->sum('eliminated_nums'); //总课时
				$elimination_sum = $elimination_sum + $item['elimination_sum'];
			}
		}

		$item_end = [];
		$item_end['campus_name'] =  '合计';
		$item_end['elimination_amount'] =  $elimination_amount;
		$item_end['elimination_no_amount'] =  $elimination_no_amount;
		$item_end['elimination_all_amount'] =  $elimination_all_amount;  //总金额;
		$item_end['elimination_sum'] = $elimination_sum; //总课时;
		$listData['data'][count($listData['data'])] = $item_end;

		return $listData;
	}

	/****统计跟进状态***/
	public function getFollowStatisticsList($param){
		$page = isset($param['page']) ? (int) $param['page'] : 1;
		$listRows = isset($param['limit']) ? (int) $param['limit'] : 10;
		$where = [];
		$where['status'] = 1;
		$where['wtype'] = 5;

		$StudentModel = new StudentModel();
		$followUpLogModel = new FollowUpLogModel();
		$userModel = new UserModel();
		$listData = $userModel::where($where)
			->paginate($listRows, false, ['page' => $page])
			->toArray();

		$today = 0;
		$yesterday = 0;
		$week = 0;
		$lastweek = 0;
		$month = 0;
		$lastmonth = 0;
		$year  =0;
		$lastyear  =0;

		$where_log = [];
		$where_log['type'] = 2;

		if (!empty($listData)) {
			foreach ($listData['data'] as &$item) {
				//今日跟进，昨天跟进，本周跟进，上周跟进，本月跟进，上月跟进，今年跟进，去年跟进
				$where_log['user_id'] = $item['id'];

				$item['today'] = $followUpLogModel->where($where_log)->whereTime('time', 'today')->count(); //今日跟进
				$today = $today + $item['today'];

				$item['yesterday'] = $followUpLogModel->where($where_log)->whereTime('time', 'yesterday')->count(); //昨天跟进
				$yesterday = $yesterday + $item['yesterday'];

				$item['week'] = $followUpLogModel->where($where_log)->whereTime('time', 'week')->count(); //本周跟进
				$week = $week + $item['week'];

				$item['lastweek'] = $followUpLogModel->where($where_log)->whereTime('time', 'last week')->count(); //上周跟进
				$lastweek = $lastweek + $item['lastweek'];

				$item['month'] = $followUpLogModel->where($where_log)->whereTime('time', 'month')->count(); //本月跟进
				$month = $month + $item['month'];

				$item['lastmonth'] = $followUpLogModel->where($where_log)->whereTime('time', 'last month')->count(); //上月跟进
				$lastmonth = $lastmonth + $item['lastmonth'];

				$item['year'] = $followUpLogModel->where($where_log)->whereTime('time', 'year')->count(); //今年跟进
				$year = $year + $item['year'];

				$item['lastyear'] = $followUpLogModel->where($where_log)->whereTime('time', 'last year')->count(); //去年跟进
				$lastyear = $lastyear + $item['lastyear'];
			}
		}

		$item_end['name'] =  '合计';
		$item_end['today'] = $today;
		$item_end['yesterday'] = $yesterday;
		$item_end['week'] = $week;
		$item_end['lastweek']  = $lastweek;
		$item_end['month'] = $month;
		$item_end['lastmonth'] = $lastmonth;
		$item_end['year'] = $year;
		$item_end['lastyear'] = $lastyear;
		$listData['data'][count($listData['data'])] = $item_end;

		return $listData;

	}
}
